﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Converter;
using csla4Skeleton.UI.SL.Resources.Utility;
using csla4Skeleton.UI.SL.DanhMuc.DM_HangHoa;
using csla4Skeleton.UI.SL.DanhMuc.DM_TinhChatHangHoa;
using csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.DM_LoaiDoiTuongTHCP;
using Accounting.Library;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using System.ComponentModel;
using Utility;
using Accounting.Library.Utility;

namespace csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.DM_DoiTuongTHCP
{
    public partial class DM_DoiTuongTHCP_MainWindow: UserControl, Csla.Xaml.ISupportNavigation
    {

        #region ISupportNavigation Members
        List<Control> controls = new List<Control>();
        public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            try
            {
                if (parameters == "")
                    Khoa = null;
                else
                    Khoa = Int32.Parse(parameters);
            }
            catch
            {
                Khoa = null;
            }
        }

        public string Title
        {
            get { return "DoiTuongTHCP"; }
        }

       
        #endregion

        #region Load Methods
        private System.Nullable<int> Khoa = null;
      
        public DM_DoiTuongTHCP_MainWindow(int Khoa)          
        {
            this.Khoa = Khoa;
        }

        public DM_DoiTuongTHCP_MainWindow()
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.DM_DoiTuongTHCP.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
            
            InitializeComponent();
            ControlBehavior.ControlBehaviors.Navigation(this.DM_DoiTuongTHCP_GeneralInformation_GroupBox, controls);
            ControlBehavior.ControlBehaviors.Navigation(this.DM_DoiTuongTHCP_MoreInformation_GroupBox, controls);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                
                System.Windows.Data.CollectionViewSource DM_DoiTuongTHCP_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_DoiTuongTHCP_ViewModelSource"];
                if (Khoa == null)
                {
                    DM_DoiTuongTHCP_ViewModel DM_DoiTuongTHCP_ViewModel = new DM_DoiTuongTHCP_ViewModel();
                    DM_DoiTuongTHCP_ViewModel.ChildList_AddedNew += new DM_DoiTuongTHCP_ViewModel.ChildList_AddedNew_EventHandler(DM_DoiTuongTHCP_ViewModel_ChildList_AddedNew);                   
                    DM_DoiTuongTHCP_ViewModel.TCHangHoaChanged += new EventHandler(DM_DoiTuongTHCP_TinhChatHangHoaChanged);
                    DM_DoiTuongTHCP_ViewModel.hangHoaIdChanged += new DM_DoiTuongTHCP_ViewModel.HangHoaIdChanged_EventHandler(DM_DoiTuongTHCP_HangHoaIdChanged);
                    DM_DoiTuongTHCP_CollectionViewSource.Source = new List<object> { DM_DoiTuongTHCP_ViewModel };

                    
                }
                else
                {
                    DM_DoiTuongTHCP_ViewModel DM_DoiTuongTHCP_ViewModel = new DM_DoiTuongTHCP_ViewModel((int)Khoa);
                    DM_DoiTuongTHCP_ViewModel.ChildList_AddedNew += new DM_DoiTuongTHCP_ViewModel.ChildList_AddedNew_EventHandler(DM_DoiTuongTHCP_ViewModel_ChildList_AddedNew);
                    DM_DoiTuongTHCP_ViewModel.TCHangHoaChanged += new EventHandler(DM_DoiTuongTHCP_TinhChatHangHoaChanged);
                    DM_DoiTuongTHCP_ViewModel.hangHoaIdChanged += new DM_DoiTuongTHCP_ViewModel.HangHoaIdChanged_EventHandler(DM_DoiTuongTHCP_HangHoaIdChanged);
                   
                    DM_DoiTuongTHCP_CollectionViewSource.Source = new List<object> { DM_DoiTuongTHCP_ViewModel };

                    
                }

                System.Windows.Data.CollectionViewSource dm_LoaiDoiTuongTHCP_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiDoiTuongTHCP_ListViewModelSource"];
                DM_LoaiDoiTuongTHCP_ListViewModel dm_LoaiDoiTuongTHCP_ListViewModel = new DM_LoaiDoiTuongTHCP_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_LoaiDoiTuongTHCP_CollectionViewSource.Source = new List<object> { dm_LoaiDoiTuongTHCP_ListViewModel };
                

                System.Windows.Data.CollectionViewSource dm_DoiTuongTHCP_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_DoiTuongTHCP_ListViewModelSource"];
                DM_DoiTuongTHCP_ListViewModel dm_DoiTuongTHCP_ListViewModel = new DM_DoiTuongTHCP_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_DoiTuongTHCP_CollectionViewSource.Source = new List<object> { dm_DoiTuongTHCP_ListViewModel };

               
                
                System.Windows.Data.CollectionViewSource dm_TinhChatHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TinhChatHangHoa_ListViewModelSource"];
                DM_TinhChatHangHoa_ListViewModel dm_TinhChatHangHoa_ListViewModel = new DM_TinhChatHangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TinhChatHangHoa_CollectionViewSource.Source = new List<object> { dm_TinhChatHangHoa_ListViewModel };


               
                DoFilterOnDefaultDM_SanPham();

                this.DM_DoiTuongTHCP_ThanhPhamGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.DM_DoiTuongTHCP_ThanhPhamGridView);
               
                
           
            }
        }

        #endregion
       

        #region Event Handle
        private void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                DM_DoiTuongTHCP_ViewModel vm = GetViewModel();
                if (e.Key == Key.S)
                {
                    if (vm.CanSave)
                    {
                        vm.Save(sender, new Csla.Xaml.ExecuteEventArgs());
                    }
                }
                else if (e.Key == Key.F6)
                {
                    AddItems();
                }
                else if (e.Key == Key.D1)
                {
                    this.DM_DoiTuongTHCP_ThanhPhamTab.IsSelected = true;
                }
                
                
            }
            e.Handled = true;
        }

        void AddItemButton_Click(object sender, RoutedEventArgs e)
        {

            AddItems();
        }

        void DeleteItemButton_Click(object sender, RoutedEventArgs e)
        {
            DeleteItems();
          
        }

        public void AddItems()
        {
           
            AddDoiTuongTHCPDetailsItem();
            
        }
        private RadGridView GetActiveGridView()
        {

            return DM_DoiTuongTHCP_ThanhPhamGridView;
            
     
        }



        void DeleteItems()
        {
            DM_DoiTuongTHCP_ThanhPhamChild ThanhPhamChild = (DM_DoiTuongTHCP_ThanhPhamChild)(this.DM_DoiTuongTHCP_ThanhPhamGridView.SelectedItem);
            IList<DM_DoiTuongTHCP_ThanhPhamChild> list = GetViewModel().Model.DM_DoiTuongTHCP_ThanhPhams;
            int index = 0;
            foreach (DM_DoiTuongTHCP_ThanhPhamChild item in list)
            {
                if (ThanhPhamChild.GetHashCode() == item.GetHashCode())
                {
                    break;
                }
                index++;
            }
            DeleteThanhPhamDetailsItem(index);
            
        }
           
        void GridView_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
        {
            var grid = e.OwnerGridViewItemsControl;
            if (grid.Items.Count > 0)
            {
                GridViewUtil.FocusRow((RadGridView)grid, grid.Items.Count - 1);
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[grid.Items.Count - 1], grid.Columns[0]);
                    grid.Focus();
                    grid.BeginEdit();
                }));
            }
        }
              
        private void SaveAndNewButton_Click(object sender, RoutedEventArgs e)
        {
           DM_DoiTuongTHCP_ViewModel vm = GetViewModel();
            vm.SaveAndNew();
        }

        private void DatePicker_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    this.AddDoiTuongTHCPDetailsItem();
                }
            }
            else
            {
                ControlBehavior.DatePickerBehaviors.DatePicker_KeyUp(sender, e, controls);
            }
        }

        private void ComboBox_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    this.AddDoiTuongTHCPDetailsItem();
                }
            }
            else
            {
                ControlBehavior.ComboBoxBehaviors.ComboBox_KeyUp(sender, e, controls);
            }
        }

        private void Control_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    this.AddDoiTuongTHCPDetailsItem();
                }
            }
            else
            {
                ControlBehavior.ControlBehaviors.Control_KeyUp(sender, e, controls);
            }
        }


      
        private void DM_DoiTuongTHCP_GeneralInformation_LoaiDoiTuongTHCPId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_LoaiDoiTuongTHCP_ChildWindow dm_LoaiDoiTuongTHCP_ChildWindow = new DM_LoaiDoiTuongTHCP_ChildWindow();
            dm_LoaiDoiTuongTHCP_ChildWindow.Show();
            dm_LoaiDoiTuongTHCP_ChildWindow.SaveClicked += new EventHandler(dm_LoaiDoiTuongTHCP_ViewModel_Saved);
        }

        private void DM_DoiTuongTHCP_GeneralInformation_ThuocDoiTuongTHCPId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {

        }

        #endregion

        #region View Model Related Methods
        private DM_DoiTuongTHCP_ViewModel GetViewModel()
        {
            System.Windows.Data.CollectionViewSource DM_DoiTuongTHCP_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_DoiTuongTHCP_ViewModelSource"];
            return ((List<object>)DM_DoiTuongTHCP_CollectionViewSource.Source)[0] as DM_DoiTuongTHCP_ViewModel;
        }

        void dm_LoaiDoiTuongTHCP_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_LoaiDoiTuongTHCP_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiDoiTuongTHCP_ListViewModelSource"];
            DM_LoaiDoiTuongTHCP_ListViewModel vm = ((List<object>)dm_LoaiDoiTuongTHCP_ListViewSource.Source)[0] as DM_LoaiDoiTuongTHCP_ListViewModel;
            vm.GetDM_LoaiDoiTuongTHCP_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        void dm_ThuocDoiTuongTHCP_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_ThuocDoiTuongTHCP_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_DoiTuongTHCP_ListViewModelSource"];
            DM_DoiTuongTHCP_ListViewModel vm = ((List<object>)dm_ThuocDoiTuongTHCP_ListViewSource.Source)[0] as DM_DoiTuongTHCP_ListViewModel;
            vm.GetDM_DoiTuongTHCP_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }
       

        public void DoFilterOnDefaultDM_SanPham()
        {
           
            System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
            DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = new DM_HangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
            dm_HangHoa_CollectionViewSource.Source = new List<object> { dm_HangHoa_ListViewModel };
           
        }
     

       
        void DM_DoiTuongTHCP_TinhChatHangHoaChanged(object sender, EventArgs e)
        {
            int? tinhChatHangHoaId = this.GetViewModel().Model.TinhChatHangHoaId;
            
            System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
            DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = ((List<object>)dm_HangHoa_CollectionViewSource.Source)[0] as DM_HangHoa_ListViewModel;
            dm_HangHoa_ListViewModel.GetDM_HangHoa((int)tinhChatHangHoaId);

        }

        
        void DM_DoiTuongTHCP_HangHoaIdChanged(object sender, HangHoaIdChangedEventArgs e)
        {

            DM_DoiTuongTHCP_ThanhPhamChild ThanhPhamChild = GetViewModel().Model.DM_DoiTuongTHCP_ThanhPhams[e.Index];
            System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
            DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = ((List<object>)dm_HangHoa_CollectionViewSource.Source)[0] as DM_HangHoa_ListViewModel;
            System.Collections.Generic.IList<DM_HangHoaInfo> list = dm_HangHoa_ListViewModel.Model.getListOfInfo();  
            foreach (DM_HangHoaInfo item in list)
            {
                if (item.Khoa == e.HangHoaId)
                {
                    ThanhPhamChild.DienGiai = item.TenViet;
                    break;
                }
            }
        }
     
        private void AddDoiTuongTHCPDetailsItem()
        {
            GetViewModel().AddDM_DoiTuongTHCP_ThanhPhamItem();
        }


        private void DeleteThanhPhamDetailsItem(int Index)
        {
            GetViewModel().DeleteDM_DoiTuongTHCP_ThanhPhamItem(Index);
        }

        void DM_DoiTuongTHCP_ViewModel_ChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            this.DM_DoiTuongTHCP_ThanhPhamGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
  
        }

        #endregion

       
        
    }
}
